package com.android.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 6. Z 字形变换
 * 将一个给定字符串 s 根据给定的行数 numRows ，以从上往下、从左到右进行 Z 字形排列。
 * 找规律的题目真的是恶心
 * https://leetcode-cn.com/problems/zigzag-conversion/solution/z-zi-xing-bian-huan-by-leetcode-solution-4n3u/
 */
class Solution6 {
    public static void main(String[] args) {
        String s = "AA";
        String result = convert(s, 1);
        System.out.println(result);
    }

    public static String convert(String s, int numRows) {
        if (s.length() == 1 || numRows == 1) {
            return s;
        }
        List<Character>[] all = new List[numRows];
        for (int i = 0; i < numRows; i++) {
            List<Character> row = new ArrayList<>();
            all[i] = row;
        }
        int step = numRows + numRows - 2;

        for (int i1 = 0; i1 < s.length(); i1 = i1 + step) {
            for (int i2 = 0; i2 < numRows; i2++) {
                int index = i1 + i2;
                if (index < s.length()) {
                    all[i2].add(s.charAt(index));
                    int nextOneRowIndex = i1 + step;
                    if (i2 != 0 && i2 != numRows - 1) {
                        if (nextOneRowIndex - i2 < s.length()) {
                            all[i2].add(s.charAt(nextOneRowIndex - i2));
                        }
                    }
                }
            }
        }
        String result = "";
        for (int i = 0; i < all.length; i++) {
            List<Character> characters = all[i];
            for (int i1 = 0; i1 < characters.size(); i1++) {
                result = result + characters.get(i1);
            }
        }
        //PAHNAPLSIIGYIR
        return result;
    }
}
